/******************************************************************************
 * * call_log_api.c - API invoked by other modules other than call log
 *
 * *(C) Copyright 2019 Asr International Ltd.
 * * All Rights Reserved
 * ******************************************************************************/

#include "call_log_global.h"

/**
 * Call app tells call log app to add a call log when there is a missed call,
 * or dialled/received call ends.
 * param (in) Data: NV_Call_Log_Data_t *
 * return VOID
 */
VOID CallLog_Call_Add_Log(NV_Call_Log_Data_t *Data)
{
    Call_Log_Node_t *CallLog = NULL;
    INT16           BlockId  = -1;
    UINT32          Offset   = 0;

    /* allocate block */
    BlockId = Block_Alloc();

    if (BlockId < 0) /* no free block */
    {
        CallLog = _lv_ll_get_tail(&g_Call_Log_Mng.CallLogList);

        if (CallLog)
        {
            /* replace the oldest */
            BlockId = CallLog->BlockId;

            /* decrease call log count */
            Call_Log_Update_Cnt((CALL_LOG_TYPE)CallLog->Data.Type, -1);

            /* remove the oldest */
            _lv_ll_remove(&g_Call_Log_Mng.CallLogList, CallLog);
            lv_mem_free(CallLog);
        }
        else
        {
            printf("Error in CallLog_Call_Add_Log\n");
        }
    }

    /* add call log to list */
    CallLog          = Call_Log_Add(Data);
    CallLog->BlockId = BlockId;

    /* increase call log count */
    Call_Log_Update_Cnt((CALL_LOG_TYPE)Data->Type, 1);

    /* write call log to NVM */
    Offset = CallLog->BlockId * sizeof(NV_Call_Log_Data_t);
    UI_NV_Write_Req(NV_SECTION_UI_CALL_LOG, Offset, sizeof(NV_Call_Log_Data_t), (UINT8 *)Data);

    if (CALL_LOG_TYPE_MISSED == Data->Type)
    {
        if (g_Call_Log_Mng.UnreadMissedCalls < 0xFF)
        {
            g_Call_Log_Mng.UnreadMissedCalls++;

            /* write UnreadMissedCalls to NVM */
            Offset = NV_CALL_LOG_MAX_NUM * sizeof(NV_Call_Log_Data_t);
            UI_NV_Write_Req(NV_SECTION_UI_CALL_LOG, Offset, 1, &g_Call_Log_Mng.UnreadMissedCalls);
        }
    }

    Hal_Mem_Free(Data);
} /* CallLog_Call_Add_Log */

/**
 * initiate call log app
 */
VOID CallLog_Framework_Init_Req()
{
    If_Framework_Reg_Info_t *RegInfo = NULL;
    NV_UI_Call_Log_t        NvData;
    Call_Log_Node_t         *CallLogNew = NULL;
    UINT32                  Len         = 0;

    Hal_Mem_Set(&g_Call_Log_Mng, 0, sizeof(Call_Log_Mng_t));
    _lv_ll_init(&g_Call_Log_Mng.CallLogList, sizeof(Call_Log_Node_t));

    /* read NVM */
    Len = UI_NV_Read_Req(NV_SECTION_UI_CALL_LOG, 0, sizeof(NV_UI_Call_Log_t), (UINT8 *)&NvData);

    if (Len > 0)
    {
        for (UINT16 i = 0; i < NV_CALL_LOG_MAX_NUM; i++)
        {
            if (CALL_LOG_TYPE_NONE == NvData.CallLog[i].Type)
            {
                continue;
            }

            /* increase call log count */
            Call_Log_Update_Cnt((CALL_LOG_TYPE)NvData.CallLog[i].Type, 1);

            /* set block state to 'allocated' */
            Block_Set_State(i, 1);

            /* add call log to list */
            CallLogNew          = Call_Log_Add(&NvData.CallLog[i]);
            CallLogNew->BlockId = i;
        }

        /* set unread missed calls */
        g_Call_Log_Mng.UnreadMissedCalls = NvData.UnreadMissedCalls;
    }

    /* register app */
    RegInfo             = (If_Framework_Reg_Info_t *)Hal_Mem_Alloc(sizeof(If_Framework_Reg_Info_t));
    RegInfo->NameTextId = PHONE_TEXT_ID_CALLLOG;
    RegInfo->AppImage   = IMG_MENU_CALL_LOG;
    RegInfo->AppFunc    = Call_Log_Main;
    Framework_App_Register_Req(RegInfo);
} /* CallLog_Framework_Init_Req */

/**
 * Open missed call list
 */
VOID CallLog_Framework_Open_Missed_Call_List_Req()
{
    g_Call_Log_Mng.CurrCallLog = NULL;
    Display_Call_Log_Call_List(CALL_LOG_TYPE_MISSED, TRUE, ACT_ID_ANY);
}

/**
 * Open dialed number list
 */
VOID CallLog_Framework_Open_Dialed_Number_List_Req()
{
    if (0 == g_Call_Log_Mng.DialledCalls)
    {
        Display_Call_Log_No_Dialled_Numbers();
    }
    else
    {
        g_Call_Log_Mng.CurrCallLog = NULL;
        Display_Call_Log_Call_List(CALL_LOG_TYPE_DIALLED, TRUE, ACT_ID_ANY);
    }
}
